{% extends "orga/base.html" %}

{% load copyable %}
{% load compress %}
{% load i18n %}
{% load static %}

{% block extra_title %}{% translate "Users" %} :: {% endblock extra_title %}

{% block scripts %}
    {% compress js %}
        <script defer src="{% static "common/js/copy.js" %}"></script>
    {% endcompress %}
{% endblock %}

{% block content %}
    <h2>{% translate "Users" %}</h2>

    <form class="mb-3 mt-2">
        <input name="q" type="text" {% if request.GET.q %} value="{{ request.GET.q }}" {% endif %} placeholder="{% translate "Search users by name or email address" %}">
    </form>

    <form method="post">
        {% csrf_token %}
        <div class="table-responsive">
            <table class="table table-sm table-flip">
                <thead>
                    <tr>
                        <th>{% translate "Name" %}</th>
                        <th>{% translate "Email" %}</th>
                        <th>{{ phrases.base.language }}</th>
                        <th>{% translate "Teams" %}</th>
                        <th>{% translate "Events" %}</th>
                        <th class="numeric">{% translate "Submissions" %}</th>
                        <th class="numeric">{% translate "Last login" %}</th>
                        <th class="numeric">{% translate "Password reset time" %}</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    {% for user in users %}
                        <tr>
                            <td>
                                <a href="{% url "orga:admin.user.view" code=user.code %}">
                                    {% include "orga/includes/user_name.html" %}
                                </a>
                            </td>
                            <td>{{ user.email|copyable }}</td>
                            <td>{{ user.locale }}</td>
                            <td>
                                <ul>
                                    {% for team in user.teams.all %}
                                        <li><a href="{{ team.urls.base }}">{{ team.name }}</a></li>
                                    {% endfor %}
                                </ul>
                            </td>
                            <td>
                                <ul>
                                    {% for event in user.get_events_with_any_permission %}
                                        <li><a href="{{ event.orga_urls.base }}">{{ event.name }}</a></li>
                                    {% endfor %}
                                </ul>
                            </td>
                            <td class="numeric">{{ user.submission_count }}</td>
                            <td class="numeric-left">{{ user.last_login|timesince }} {% if user.last_login %}ago{% endif %}</td>
                            <td class="numeric-left">{{ user.pw_reset_time|timesince }}{% if user.pw_reset_time %} ago{% endif %}</td>
                            <td>
                                <div class="d-flex mb-2">
                                    <a href="{% url "orga:admin.user.view" code=user.code %}" class="btn btn-xs btn-info mr-2"> <i class="fa fa-edit"></i> </a>
                                    <a href="{% url "orga:admin.user.delete" code=user.code %}" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i></a>
                                </div>
                                <button name="action" value="reset-{{ user.id }}" class="btn btn-xs btn-warning mr-2">{{ phrases.base.password_reset_heading }}</button>
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </form>
    </div>
    {% include "orga/includes/pagination.html" %}
{% endblock content %}
